package cate.game.play.buff.h;


import cate.common.table.d.GDBuff;
import cate.common.table.d.GDPlay;
import cate.game.play.base.HpCfg;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.result.ShieldChangeResult;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.struct.ref.DoubleRef;

@NoteClass("护盾BH")
public class 护盾BH extends BuffHandler {

    private HpCfg 血量配置;

    protected double 单次最大系数;

    //血量配置=？？&单次最大系数=？？
    @Override
    public void parse(String code) {
        PlayCfgArgs args = new PlayCfgArgs(code);
        血量配置 = new HpCfg(args);
        单次最大系数 = args.getWDouble("单次最大系数",0d);
    }

    @Override
    public void parseExt(String code) {
        血量配置.merge(new HpCfg(new PlayCfgArgs(code)));
    }


    private Double 血量调整;

    public void add血量调整(double 血量调整) {
        if (this.血量调整 == null) {
            this.血量调整 = 0d;
        }
        this.血量调整 += 血量调整;
    }

    @NoteField("护盾当前血量")
    private long shieldValue;


    @Override
    public void onAdd(ActionCtx action) {
        shieldValue = (long) (血量配置.settle(buff.caster, buff.owner));
        shieldValue += buff.cfg.getBuffHp();
        if (血量调整 != null) {
            shieldValue *= (1 + 血量调整);
        }
        if (shieldValue <= 0) {
            return;
        }
        ShieldChangeResult result = new ShieldChangeResult().pidSet(buff.owner).valSet(shieldValue).
                hpSet(buff.owner.buff.shieldValue()).reasonSet(GDPlay.ShieldReason.BUFF_CHANGE);
        action.addResult(result);
    }


    /** 剩余吸收血量 */
    public long shieldValue() {
        return shieldValue;
    }

    private void setShieldValue(long value) {
        this.shieldValue = value;
    }

    @Override
    public double handleAbsorb(ActionCtx action, DoubleRef doubleRef) {
        double totalDamage = doubleRef.v;
        if (totalDamage <= 0) {
            return 0;
        }
        if (单次最大系数 > 0) {
            totalDamage *= 单次最大系数;
        }
        long leftHp = shieldValue();
        //本次吸收量
        double absorb = 0;
        if (totalDamage >= leftHp) {
            absorb = leftHp;
        } else {
            absorb = totalDamage;
        }
        doubleRef.v -= absorb;
        leftHp -= absorb;
        if (leftHp <= 0L) {
            buff.markInvalid(action, GDBuff.InvalidReason.EXHAUSTED);
        } else {
            setShieldValue(leftHp);
        }
        return absorb;
    }

    @Override
    public void onInvalid(ActionCtx action, byte invalidReason) {
        if (shieldValue > 0) {
            ShieldChangeResult result = new ShieldChangeResult().pidSet(buff.owner).
                    valSet(-shieldValue).hpSet(buff.owner.buff.shieldValue()).reasonSet(GDPlay.ShieldReason.BUFF_CHANGE);
            action.addResult(result);
        }
    }
}
